package dto

import (
	"time"

	"gitee.com/hp-huiw/family-expense-api/internal/constant"
	"gitee.com/hp-huiw/my-go-core/date"
	"gitee.com/hp-huiw/my-go-core/util"
)

type IncomeExpenseDetail struct {
	FullAddressCoordinate
	ID          string  `json:"id,omitempty"`
	Amount      float64 `json:"amount,omitempty"`
	Type        string  `json:"type,omitempty"`
	DetailType  string  `json:"detailType,omitempty"`
	PaymentType string  `json:"paymentType,omitempty"`
	Details     string  `json:"details,omitempty"`
	Remark      *string `json:"remark,omitempty"`
}

type IncomeExpenseDetailRequest struct {
	IncomeExpenseDetail
	AuditInfo
	BankCard *struct {
		ID string `json:"id"`
	} `json:"bankCard"`
	IncomeExpenseDate string `json:"incomeExpenseDate"`
}

// The field tag is for gin ctx.BindQuery function.
type IncomeExpenseDetailQueryArgumentRequest struct {
	PageSize                int64  `form:"pageSize"`
	PageIndex               int64  `form:"pageIndex"`
	AccountGroupID          string `form:"accountGroupID"`
	Keyword                 string `form:"keyword"`
	KeywordsSearchCondition string `form:"keywordsSearchCondition"`
	Type                    string `form:"type"`
	DetailType              string `form:"detailType"`
	PaymentType             string `form:"paymentType"`
	BankCardID              string `form:"bankCardId"`
	StartDateStr            string `form:"startDate"`
	EndDateStr              string `form:"endDate"`
	StartDate               *time.Time
	EndDate                 *time.Time
}

func (r *IncomeExpenseDetailQueryArgumentRequest) Init() {
	if util.IsEmptyString(r.KeywordsSearchCondition) {
		r.KeywordsSearchCondition = constant.KeywordsSearchOrCondition
	}

	if !util.IsEmptyString(r.StartDateStr) {
		if time, err := date.ParseFromLongDateTime(r.StartDateStr); err == nil {
			r.StartDate = &time
		}
	}

	if !util.IsEmptyString(r.EndDateStr) {
		if time, err := date.ParseFromLongDateTime(r.EndDateStr); err == nil {
			r.EndDate = &time
		}
	}
}

type IncomeExpenseDetailResponse struct {
	IncomeExpenseDetail
	BankCardInfo
	AuditInfo
	BankCardID        *string    `json:"bankCardId,omitempty"`
	IncomeExpenseDate *time.Time `json:"incomeExpenseDate,omitempty"`
}

type IncomeExpenseDetailWithSummaryResponse struct {
	IncomeExpenseDetailResponse
	MonthSummary *AnalysisSummaryResponse `json:"monthSummary"`
	DaySummary   *AnalysisSummaryResponse `json:"daySummary"`
}

type IncomeExpenseBasicStatisticsResponse struct {
	TotalUserCount          int64 `json:"totalUserCount"`
	TotalBankCardCount      int64 `json:"totalBankCardCount"`
	TotalIncomeExpenseCount int64 `json:"totalIncomeExpenseCount"`
	TotalIncomeExpenseDays  int64 `json:"totalIncomeExpenseDays"`
}

type AnalysisResultResponse struct {
	CashIncome        float64 `json:"cashIncome"`
	CashExpense       float64 `json:"cashExpense"`
	CashBalance       float64 `json:"cashBalance"`
	CreditCardIncome  float64 `json:"creditCardIncome"`
	CreditCardExpense float64 `json:"creditCardExpense"`
	CreditCardBalance float64 `json:"creditCardBalance"`
}

type AnalysisSummaryResponse struct {
	IncomeExpenseDate time.Time `json:"incomeExpenseDate"`
	AnalysisResultResponse
}

type AnnualAnalysisSummaryResponse struct {
	Year string `json:"year"`
	AnalysisResultResponse
}

type DetailTypeSummaryResponse struct {
	DetailType string  `json:"detailType"`
	Amount     float64 `json:"amount"`
}
